#include "SudokuChecker.h"
#include "SudokuMaker.h"

void zeroFlag(bool* flag)
{
	for (int i = 0; i < 10; i++)
		flag[i] = false;
}

int checkGrid(Sudoku s)
{
	bool flag[10];
	for (int grid = 0; grid < 9; grid++)
	{
		zeroFlag(flag);
		int row = grid / 3 * 3 + 1;
		int col = grid % 3 * 3 + 1;
		for (int i = 0; i < 3; i++)
		{
			for (int j = 0; j < 3; j++)
			{
				if (flag[s[row + i][col + j]])
					return (row + i) * 10 + col + j;
				else
					flag[s[row + i][col + j]] = true;
			}
		}
	}
	return 0;
}

int checkRowCol(Sudoku s)
{
	bool flag_row[10], flag_col[10];
	for (int i = 1; i <= 9; i++)
	{
		zeroFlag(flag_row);
		zeroFlag(flag_col);
		for (int j = 1; j <= 9; j++)
		{
			if (flag_row[s[i][j]])
				return i * 10 + j;
			else
				flag_row[s[i][j]] = true;
			if (flag_col[s[j][i]])
				return i + j * 10;
			else
				flag_col[s[j][i]] = true;
		}
	}
	return 0;
}


int checkSudoku(Sudoku s)
{
	int err = checkGrid(s);
	if (err > 0)
		return -err;
	err = checkRowCol(s);
	return -err;
}

int checkRepeation(const Sudoku& s1, const Sudoku& s2)
{
	for (int i = 1; i < 10; i++)
	{
		for (int j = 1; j < 10; j++)
			if (s1[i][j] != s2[i][j])
				return 0;
	}
	return 1;
}
